from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.utils.functional import curry
from django.http import QueryDict

from moderation.utils import put_to_moderation
from django.http import QueryDict
from openpolitics.models import Elections,Person
from promisecheck.models import Promise, Status, PromiseForm, LogForm, Log
from .views import add_promise as add_promise_view
from .views import update_promise as update_promise_view

@login_required
def add_promise(request,
                person,
                template_name='promisecheck'):
    if not request.method == 'POST':
        return HttpResponseRedirect(reverse('promisecheck.views.index'))
    qd = QueryDict("", mutable=True)
    for k,v in request.POST.items():
        qd[k] = v
    qd['person'] = Person.objects.get(slug=person).id
    qd['elections'] = Elections.objects.all()[0].id
    form = PromiseForm(qd)
    if form.is_valid():
        put_to_moderation(request.user, Promise, 'add', form=form)
        return HttpResponseRedirect(reverse('promisecheck.views.index'))
    else:
        return add_promise_view(request,
                                person=person,
                                form=form,
                                template_name=template_name)

@login_required
def add_log(request,
            promise,
            template_name='promisecheck'):

    if not request.method == 'POST':
        return HttpResponseRedirect(reverse('promisecheck.views.index'))
    qd = QueryDict("", mutable=True)
    for k,v in request.POST.items():
        qd[k] = v
    qd['promise'] = promise

    form = LogForm(qd)
    
    if form.is_valid():
        put_to_moderation(request.user,
                          Log,
                          'add',
                          form=form,
                          onaccept='update_promise_status')
        return HttpResponseRedirect(reverse('promisecheck.views.index'))
    else:
        return update_promise_view(request,
                                   promise=promise,
                                   form=form,
                                   template_name=template_name)

def remove_promise(request,
                   template_name='promisecheck'):
    pass

def remove_entry(request,
                   template_name='promisecheck'):
    pass

def remove_contribution(request,
                        template_name='promisecheck'):
    pass
